import { ExtractJwt, Strategy } from "passport-jwt";
import { PassportStrategy } from "@nestjs/passport";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { SECRET_KEY } from "../common/env";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(protected configService: ConfigService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: configService.get<string>(SECRET_KEY),
    });
  }

  // 对token进行校验
  async validate(payload: { phone: string }) { // payload: 是解密后的token
    // 将数据加到request.user上
    return { phone: payload.phone };
  }
}